package com.ruoyi.wx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wx.domain.Account;
import com.ruoyi.wx.service.IAccountService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 账号Controller
 * 
 * @author wt
 * @date 2020-12-13
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wx/account" )
public class AccountController extends BaseController {

    private final IAccountService iAccountService;

    /**
     * 查询账号列表
     */
    @PreAuthorize("@ss.hasPermi('wx:account:list')")
    @GetMapping("/list")
    public TableDataInfo list(Account account)
    {
        startPage();
        LambdaQueryWrapper<Account> lqw = Wrappers.lambdaQuery(account);
        if (StringUtils.isNotBlank(account.getAccount())){
            lqw.eq(Account::getAccount ,account.getAccount());
        }
        if (StringUtils.isNotBlank(account.getPwd())){
            lqw.eq(Account::getPwd ,account.getPwd());
        }
        if (account.getState() != null){
            lqw.eq(Account::getState ,account.getState());
        }
        if (account.getType() != null){
            lqw.eq(Account::getType ,account.getType());
        }
        if (account.getUserInfoId() != null){
            lqw.eq(Account::getUserInfoId ,account.getUserInfoId());
        }
        if (StringUtils.isNotBlank(account.getWxId())){
            lqw.eq(Account::getWxId ,account.getWxId());
        }
        if (account.getUnblockingTime() != null){
            lqw.eq(Account::getUnblockingTime ,account.getUnblockingTime());
        }
        if (account.getSharedTenantId() != null){
            lqw.eq(Account::getSharedTenantId ,account.getSharedTenantId());
        }
        List<Account> list = iAccountService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出账号列表
     */
    @PreAuthorize("@ss.hasPermi('wx:account:export')" )
    @Log(title = "账号" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Account account) {
        LambdaQueryWrapper<Account> lqw = new LambdaQueryWrapper<Account>(account);
        List<Account> list = iAccountService.list(lqw);
        ExcelUtil<Account> util = new ExcelUtil<Account>(Account. class);
        return util.exportExcel(list, "account" );
    }

    /**
     * 获取账号详细信息
     */
    @PreAuthorize("@ss.hasPermi('wx:account:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iAccountService.getById(id));
    }

    /**
     * 新增账号
     */
    @PreAuthorize("@ss.hasPermi('wx:account:add')" )
    @Log(title = "账号" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Account account) {
        return toAjax(iAccountService.save(account) ? 1 : 0);
    }

    /**
     * 修改账号
     */
    @PreAuthorize("@ss.hasPermi('wx:account:edit')" )
    @Log(title = "账号" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Account account) {
        return toAjax(iAccountService.updateById(account) ? 1 : 0);
    }

    /**
     * 删除账号
     */
    @PreAuthorize("@ss.hasPermi('wx:account:remove')" )
    @Log(title = "账号" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iAccountService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
